package com.lanou.musicbox.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.RankingMusicAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.RankingMusic;
import com.lanou.musicbox.utils.VolleySingleton;
import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class RankingMusicFragment extends BaseFragment{

    private ListView listView;
    private StringRequest stringRequest;
    private List<RankingMusic.SongListEntity> songListEntities;
    private VolleySingleton volleySingleton;
    private RankingMusicAdapter adapter;
    private ImageView ranking_music_img;
    private TextView ranking_music_number;
    private ImageLoader imageLoader;
    private  RankingMusic rankingMusic;
    private  Toolbar toolbar;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private String URl_first = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.billboard.billList&format=json&type=";
    private String URl_end="&offset=0&size=100&fields=song_id%2Ctitle%2Cauthor%2Calbum_title%2Cpic_big%2Cpic_small%2Chavehigh%2Call_rate%2Ccharge%2Chas_mv_mobile%2Clearn%2Csong_source%2Ckorean_bb_song";
    private String URl_type ;
    private String URl_all;
    private String TAG = "lanou3344";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_ranking_music, null);
        listView = (ListView) v.findViewById(R.id.ranking_listview_music);
        ranking_music_img = (ImageView) v.findViewById(R.id.ranking_music_img);
         toolbar = (Toolbar)v.findViewById(R.id.toolbar);


        collapsingToolbarLayout = (CollapsingToolbarLayout)v.findViewById(R.id.collapsing_toolbar);
        ranking_music_img = (ImageView) v.findViewById(R.id.ranking_music_img);
        ranking_music_number = (TextView) v.findViewById(R.id.ranking_music_number);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        imageLoader = volleySingleton.getImageLoader();
        toolbar.setNavigationIcon(R.mipmap.back_btn);

        collapsingToolbarLayout.setTitleEnabled(true);
      //接收数据,获得 type_id 的值
        Bundle bundle = getArguments();
        URl_type = bundle.getString("type_id");
        initDatas();

    }
//解析数据
    private void initDatas() {
        URl_all = URl_first + URl_type + URl_end;
        Log.i(TAG, "initDatas_URl_all " + URl_all);
        stringRequest = new StringRequest(URl_all, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                rankingMusic = gson.fromJson(s,RankingMusic.class);
                if (rankingMusic !=null){
                   songListEntities = rankingMusic.getSong_list();
                    Log.i(TAG, "onResponse "+ songListEntities.size());
                    adapter = new RankingMusicAdapter(songListEntities,getActivity());
                    initView();
                    listView.setAdapter(adapter);

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        volleySingleton.addQueue(stringRequest);
    }

    private void initView() {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(ranking_music_img,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
      //  Palette palette = Palette.generate();

        imageLoader.get(rankingMusic.getBillboard().getPic_s260(), listener);
        ranking_music_number.setText(rankingMusic.getSong_list().size()+"");

        collapsingToolbarLayout.setTitle(rankingMusic.getBillboard().getName());

    }
}
